﻿Imports System.Data
Imports ADOEntityKYC
Imports ADOEntityAMLO101
Imports ADOEntity

Partial Class AMLO_Amlo1_01_CBS
    Inherits Web.UI.BasePage
    Private Const strMenuCode As String = "M51"
    Dim dsAmlo As DataSet
    Dim clsDataTable As clsLINQToDataTable
    Dim AMLOEntity As AMLO101Entities
    Dim intBeforeItemIndex As Integer
    Dim clsSessionMenuRight As clsSessionMenuRight
    Dim clsFormatDate As clsDateFormat
    Dim clsMasterDropDown As clsMasterDropDown

#Region "Even From "
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        chkIsLogin()
        btnSearch.Attributes.Add("onclick", "NoDoubleSubmit();")
        If (Not IsPostBack) Then
            AMLOEntity = New AMLO101Entities
            clsMasterDropDown = New clsMasterDropDown
            clsSessionMenuRight = Session("ClsMenuRight")
            dsAmlo = New DataSet()
            CreateControlMP()
            loadDropDown()
            setUserRightButton()
            setDefaultSearch()


            AMLOEntity.Dispose()
            AMLOEntity = Nothing
        Else
            dsAmlo = Session("ds")
            'grdAmlo.DataSource = dsAmlo.Tables("Amlo")
            'grdAmlo.DataBind()
        End If
    End Sub

    Protected Sub Page_Unload(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Unload
        Session("ds") = dsAmlo
    End Sub
#End Region

#Region "SetForm"

    Sub resourceSet()

    End Sub
#End Region

#Region "Even Button"
    Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        lblMsg.Visible = False
        lblMsg.Text = ""
        IsDataBind()
        SaveLogSearch()
        clearGvIndex()
    End Sub

    Protected Sub btnPrint_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPrint.Click
        Dim param As String = "{ 'RPTTYPE' : 'Amlo101MapCbs'," _
                                    & "'DEPTCODE' : '" & ddlDepartment.SelectedValue & "'," _
                                    & "'ZONECODE' : '" & ddlZone.SelectedValue & "'," _
                                    & "'BRANCHCODE' : '" & ddlBranch.SelectedValue & "'," _
                                    & "'STARTDATE' : '" & txtDateStart.Text.Trim & "'," _
                                    & "'ENDDATE' : '" & txtDateEnd.Text.Trim & "'}"
        SaveLogPrint(2)

        ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType, "key", "OpenWindowWithPost('../Form/frmReportAmloCBS.aspx','width=730,height=345,left=100,top=100,resizable=yes,scrollbars=yes','Report', " + param + ");", True)

    End Sub

    Protected Sub btnPrintCBS_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPrintCBS.Click
        Dim param As String = "{ 'RPTTYPE' : 'Amlo101Cbs'," _
                                    & "'DEPTCODE' : '" & ddlDepartment.SelectedValue & "'," _
                                    & "'ZONECODE' : '" & ddlZone.SelectedValue & "'," _
                                    & "'BRANCHCODE' : '" & ddlBranch.SelectedValue & "'," _
                                    & "'STARTDATE' : '" & txtDateStart.Text.Trim & "'," _
                                    & "'ENDDATE' : '" & txtDateEnd.Text.Trim & "'}"
        SaveLogPrint(3)

        ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType, "key", "OpenWindowWithPost('../Form/frmReportAmloCBS.aspx','width=730,height=345,left=100,top=100,resizable=yes,scrollbars=yes','Report', " + param + ");", True)

    End Sub

    Protected Sub btnMapping_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim btnMapping As ImageButton = CType(sender, ImageButton)
        Dim strCbsSeq = CType(btnMapping.Parent.Parent.FindControl("hdfCbsSeq"), HiddenField).Value
        Dim strCbsTranDate = CType(btnMapping.Parent.Parent.FindControl("hdfCbsTranDate"), HiddenField).Value

        Dim strCbsSolId = CType(btnMapping.Parent.Parent.FindControl("hdfCbsSolId"), HiddenField).Value

        clsFormatDate = New clsDateFormat()
        Dim cbsTranDate As Date = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, strCbsTranDate)
        Dim clsCultureInfo = New clsCultureInfo()
        strCbsTranDate = clsCultureInfo.EnDate(cbsTranDate)

        Dim param As String = "CBS_SEQ=" & strCbsSeq _
                                    & "&SOL_ID=" & strCbsSolId _
                                    & "&CBS_TRAN_DATE=" & strCbsTranDate

        ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType, "onclick", _
                              "window.showModalDialog('Amlo1_01_Popup.aspx?" & param & "'" _
                                                              & ", null, 'status:no; dialogWidth:900px; dialogHeight:600px; help:no; scroll:yes; menubar:no; resizable:no'); document.getElementById('" & btnSearch.ClientID & "').click();", True)


    End Sub


#End Region

#Region "Event Dropdown"
    Protected Sub ddlDepartment_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlDepartment.SelectedIndexChanged
        Dim clsDropDown As New clsMasterDropDown
        clsSessionMenuRight = Session("ClsMenuRight")

        Try
            If Not (ddlDepartment.SelectedValue = "0") Then
                clsDropDown.setZoneToDdl(ddlData:=ddlZone, strDeptCode:=ddlDepartment.SelectedValue, strRoleCode:=clsSessionMenuRight.getRoleGroup, strZoneCode:=clsSessionMenuRight.getZoneCode)
                clsDropDown.setBranchUseAllCodeToDdl(ddlBranch, strDeptCode:=ddlDepartment.SelectedValue, strBranchCode:=clsSessionMenuRight.getBranchCode, strRoleCode:=clsSessionMenuRight.getRoleGroup, strZoneCode:=clsSessionMenuRight.getZoneCode)
            Else
                clsDropDown.setZoneToDdl(ddlData:=ddlZone, strDeptCode:=ddlDepartment.SelectedValue, strRoleCode:=clsSessionMenuRight.getRoleGroup, strZoneCode:=clsSessionMenuRight.getZoneCode)
                ddlZone.SelectedValue = "ZZZ"
                clsDropDown.setBranchUseAllCodeToDdl(ddlBranch, strDeptCode:=ddlDepartment.SelectedValue, strBranchCode:=clsSessionMenuRight.getBranchCode, strRoleCode:=clsSessionMenuRight.getRoleGroup, strZoneCode:=clsSessionMenuRight.getZoneCode)
            End If

            clsDropDown = Nothing
        Catch ex As Exception
            WriteError(ex, "ddlDepartment_SelectedIndexChanged", "Amlo1_01_CBS")
        End Try
    End Sub

    Protected Sub ddlZone_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlZone.SelectedIndexChanged
        Dim clsDropDown As New clsMasterDropDown
        clsSessionMenuRight = Session("ClsMenuRight")

        Try
            If (ddlZone.SelectedValue = "0") Then
                clsDropDown.setBranchUseAllCodeToDdl(ddlBranch, strDeptCode:=ddlDepartment.SelectedValue, strBranchCode:=clsSessionMenuRight.getBranchCode, strRoleCode:=clsSessionMenuRight.getRoleGroup, strZoneCode:=clsSessionMenuRight.getZoneCode)
            Else
                clsDropDown.setBranchUseAllCodeToDdl(ddlBranch, strDeptCode:=ddlDepartment.SelectedValue, strBranchCode:=clsSessionMenuRight.getBranchCode, zoneCodeUse:=ddlZone.SelectedValue, strRoleCode:=clsSessionMenuRight.getRoleGroup, strZoneCode:=clsSessionMenuRight.getZoneCode)
            End If

            clsDropDown = Nothing
        Catch ex As Exception
            WriteError(ex, "ddlZone_SelectedIndexChanged", "Amlo1_01_CBS")
        End Try
    End Sub
#End Region


#Region "Even GridView"

    Protected Sub grdAmlo_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles grdAmlo.PreRender
        If (Not IsNothing(grdAmlo.HeaderRow)) Then
            grdAmlo.DataSource = dsAmlo.Tables("Amlo")
            grdAmlo.DataBind()

            Dim row = New GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal)
            Dim cell = New TableHeaderCell()
            cell.Text = "ธุรกรรมที่เกิดขึ้นจริง"
            cell.ColumnSpan = 6
            row.Controls.Add(cell)

            cell = New TableHeaderCell()
            cell.Text = ""
            cell.ColumnSpan = 1
            cell.BackColor = Drawing.Color.Black
            cell.BorderColor = Drawing.Color.Black
            row.Controls.Add(cell)

            cell = New TableHeaderCell()
            cell.Text = "ธุรกรรมที่สาขาบันทึก"
            cell.ColumnSpan = 9
            'cell.Width = Unit.Percentage(50)
            row.Controls.Add(cell)

            'row.BackColor = Drawing.ColorTranslator.FromHtml("#3AC0F2")
            grdAmlo.HeaderRow.Parent.Controls.AddAt(0, row)

        End If
    End Sub

    Protected Sub grdAmlo_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdAmlo.RowDataBound
        clsFormatDate = New clsDateFormat
        If (e.Row.RowType.ToString = "DataRow") Then
            'If Not (e.Row.Cells(2).Text = Nothing) Then
            '    e.Row.Cells(2).Text = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.Th, e.Row.Cells(2).Text)
            'End If
            'If Not (e.Row.Cells(10).Text = Nothing) Then
            '    e.Row.Cells(10).Text = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.Th, e.Row.Cells(10).Text)
            'End If
        End If
    End Sub

    Protected Sub grdAmlo_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles grdAmlo.PageIndexChanging
        grdAmlo.PageIndex = e.NewPageIndex
        clearGvIndex()
        DataBind()
    End Sub

    Protected Sub grdAmlo_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles grdAmlo.Sorting
        Dim dt = TryCast(dsAmlo.Tables("Amlo"), DataTable)

        If dt IsNot Nothing Then

            'Sort the data.
            dt.DefaultView.Sort = e.SortExpression & " " & GetSortDirection(e.SortExpression)
            grdAmlo.DataSource = dsAmlo.Tables("Amlo")
            grdAmlo.DataBind()
            clearGvIndex()
        End If
    End Sub

    Protected Sub chkAll_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)

        Dim chk As CheckBox = CType(grdAmlo.HeaderRow.FindControl("chkAll"), CheckBox)
        If (chk.Checked) Then
            For i As Int16 = 0 To grdAmlo.Rows.Count - 1
                Dim chkrow As CheckBox = CType(grdAmlo.Rows(i).FindControl("CheckBox1"), CheckBox)
                chkrow.Checked = True

            Next
        Else

            For i As Int16 = 0 To grdAmlo.Rows.Count - 1
                Dim chkrow As CheckBox = CType(grdAmlo.Rows(i).FindControl("CheckBox1"), CheckBox)
                chkrow.Checked = False

            Next

        End If
    End Sub
#End Region

#Region " Function "

    Private Sub SaveLogPrint(ByVal index As Integer)
        Try
            Dim program As String = strMenuCode
            Dim desc As String = ""
            Dim strUnique As String = "033"

            clsSessionMenuRight = Session("ClsMenuRight")

            If index = 1 Then
                desc = "ค้นหาข้อมูลจาก "
                If Not (ddlBranch.SelectedIndex = 0) Then
                    desc = desc & " ส่วนงาน/สาขา " & ddlBranch.Text
                End If

                If Not (txtDateStart.Text = "" And txtDateEnd.Text = "") Then
                    desc = desc & " วันที่ทำธุรกรรม " & txtDateStart.Text & " ถึง " & txtDateEnd.Text
                End If

                If Not (rdoReportStatus.SelectedValue = -1) Then
                    desc = desc & " สถานะรายงาน " & rdoReportStatus.Text
                End If


            ElseIf index = 2 Then
                desc = "พิมพ์ รายงานตรวจสอบการทำธุรกรรมที่ใช้เงินสด Amlo101_Map_CBS"
            Else
                desc = "พิมพ์ รายงานการทำธุรกรรมที่ใช้เงินสดที่เกิดขึ้นจริง Amlo101_CBS"
            End If

            Dim clsLogTransaction As New clsLogTransaction
            clsLogTransaction.saveLogTransaction("", clsSessionMenuRight.getUserName, index, desc, program)
            clsLogTransaction = Nothing
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Private Sub setUserRightButton()
        Dim col As New Collection

        col.Add(btnSearch, statusbtn.Query.ToString)
        'col.Add(btnVerify, statusbtn.Modify.ToString)
        col.Add(btnPrint, statusbtn.print.ToString)
        setBtnToUserRight(col, strMenuCode, clsSessionMenuRight.getDsMenuRight)

    End Sub

    Private Sub loadDropDown()
        clsMasterDropDown = New clsMasterDropDown
        If (clsSessionMenuRight.getRoleGroup > 79) Then
            clsMasterDropDown.setDeptToDdlSearch(ddlDepartment, _
                                       strDeptCode:="0", _
                                       strRoleCode:=clsSessionMenuRight.getRoleGroup)

            clsMasterDropDown.setZoneToDdl(ddlZone, selectData:="0", strRoleCode:=clsSessionMenuRight.getRoleGroup, _
                                           strZoneCode:=clsSessionMenuRight.getZoneCode, strDeptCode:=ddlDepartment.SelectedValue)

            clsMasterDropDown.setBranchUseAllCodeToDdl(ddlBranch, strDeptCode:="0", _
                                            strBranchCode:=clsSessionMenuRight.getBranchCode, _
                                            strRoleCode:=clsSessionMenuRight.getRoleGroup, _
                                            strZoneCode:="0", _
                                            selectData:=clsSessionMenuRight.getBranchCode)

        Else
            clsMasterDropDown.setDeptToDdlSearch(ddlDepartment, _
                                       strDeptCode:=clsSessionMenuRight.getDeptCode, _
                                       strRoleCode:=clsSessionMenuRight.getRoleGroup)

            clsMasterDropDown.setZoneToDdl(ddlZone, selectData:=clsSessionMenuRight.getZoneCode, strRoleCode:=clsSessionMenuRight.getRoleGroup, _
                                           strZoneCode:=clsSessionMenuRight.getZoneCode, strDeptCode:=ddlDepartment.SelectedValue)

            clsMasterDropDown.setBranchDdlSearch(ddlBranch, strDeptCode:=clsSessionMenuRight.getDeptCode, _
                                                        strBranchCode:=clsSessionMenuRight.getBranchCode, _
                                                        strRoleCode:=clsSessionMenuRight.getRoleGroup, _
                                                        strZoneCode:=clsSessionMenuRight.getZoneCode, _
                                                        selectData:=clsSessionMenuRight.getBranchCode)

        End If

    End Sub

    Public Sub setDefaultSearch()
        clsFormatDate = New clsDateFormat
        txtDateStart.Text = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.Th, dteDateIsChk:=Date.Now)
        txtDateEnd.Text = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.Th, dteDateIsChk:=Date.Now)

        rdoReportStatus.SelectedValue = "-"

        If (clsSessionMenuRight.getRoleGroup > 79) Then
            btnPrintCBS.Visible = True
        ElseIf (clsSessionMenuRight.getRoleGroup < 80) Then
            btnPrintCBS.Visible = False
        End If

        If (clsSessionMenuRight.getRoleGroup > 59) Then
            ddlBranch.Enabled = True
        ElseIf (clsSessionMenuRight.getRoleGroup < 60) Then
            ddlBranch.Enabled = False
        End If

    End Sub

    Public Sub CreateControlMP() 'อ้าง control ใน masterPage
        Dim img As Image = CType(Master.FindControl("imgHead"), Image)
        img.ImageUrl = "~/img/amlo01_01.jpg"

        Dim lbl As Label = CType(Master.FindControl("lblPage"), Label)
        lbl.Text = "ตรวจสอบข้อมูลธุรกรรม ปปง.1-01"
    End Sub

    Private Sub setBranch() 'ตั้งค่าหน้าเว็บ

        Dim qryBranch = From b In AMLOEntity.SBRANCH
                           Select b.BRANCHCODE, b.BRANCHNAME

        ddlBranch.DataSource = qryBranch
        ddlBranch.DataTextField = "BRANCHNAME"
        ddlBranch.DataValueField = "BRANCHCODE"
        ddlBranch.DataBind()
        ddlBranch.Items.Insert(0, "** กรุณาเลือกสาขา **")

        qryBranch = Nothing


    End Sub

    Private Sub IsDataBind()
        clsFormatDate = New clsDateFormat
        Dim UserRight As clsSessionMenuRight
        UserRight = Session("clsMenuRight")
        Dim strSql As String

        If dsAmlo.Tables.Contains("Amlo") Then
            dsAmlo.Tables.Clear()
        End If

        strSql = "select * from V_AMLO101_CBS t where 1=1 "

        If Not (ddlDepartment.SelectedValue = "0") Then
            strSql = strSql & " and ( cbs_deptcode = '" & ddlDepartment.SelectedValue & "' or deptcode = '" & ddlDepartment.SelectedValue & "')"
        End If

        If Not (ddlZone.SelectedValue = "0") Then
            strSql = strSql & " and ( cbs_zonecode = '" & ddlZone.SelectedValue & "' or zonecode = '" & ddlZone.SelectedValue & "')"
        End If

        If Not (ddlBranch.SelectedValue = "0") Then
            strSql = strSql & " and ( cbs_branchcode = '" & ddlBranch.SelectedValue & "' or branchcode = '" & ddlBranch.SelectedValue & "')"
        End If

        If Not (txtDateStart.Text = "" AndAlso txtDateEnd.Text = "") Then
            Dim dteStartDate As Date = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtDateStart.Text) & " 00:00:00"
            Dim dteEndDate As Date = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtDateEnd.Text) & " 23:59:59"
            Dim clsCultureInfo = New clsCultureInfo()
            Dim strStartDate = clsCultureInfo.EnDate(dteStartDate)
            Dim strEndDate = clsCultureInfo.EnDate(dteEndDate)

            strSql = strSql & " and ((cbs_tran_date >= to_date('" & strStartDate & "', 'yyyymmdd') and cbs_tran_date <= to_date('" & strEndDate & "', 'yyyymmdd'))"
            strSql = strSql & " or (transdate >= to_date('" & strStartDate & "', 'yyyymmdd') and transdate <= to_date('" & strEndDate & "', 'yyyymmdd')))"
        End If

        If Not (rdoReportStatus.SelectedValue = "-") Then
            If (rdoReportStatus.SelectedValue = "") Then
                strSql = strSql & " and cbs_report_flg is null"
            Else
                strSql = strSql & " and cbs_report_flg = '" & rdoReportStatus.SelectedValue & "'"
            End If
        End If

        Dim clsDatabase = New clsDatabase()
        Dim dt = clsDatabase.QueryDataTable(strSql)
        dt.TableName = "Amlo"
        dsAmlo.Tables.Add(dt)

        If (dt.Rows.Count = 0) Then
            lblMsg.Visible = True
            lblMsg.Text = "ไม่พบข้อมูล"
            lblTotal.Text = ""
            lblTotalCbs.Text = ""
        Else
            lblMsg.Visible = False
            lblMsg.Text = ""
            Dim cbsCount = dt.Compute("Count(CBS_SEQ)", "")
            Dim rptCount = dt.Compute("Count(RPTSEQ)", "")

            lblTotalCbs.Text = "จำนวนธุรกรรมที่เกิดขึ้นจริง " + cbsCount.ToString + " รายการ "
            lblTotal.Text = "จำนวนรายการที่สาขาบันทึก " + rptCount.ToString + " รายการ "
            'lblTotal.Text = "จำนวนรายการทั้งหมด " + dt.Rows.Count.ToString + " รายการ "
        End If

        grdAmlo.DataSource = dsAmlo.Tables("Amlo")
        grdAmlo.DataBind()
    End Sub

    Private Sub clearGvIndex()
        grdAmlo.SelectedIndex = -1
    End Sub

    Private Function GetSortDirection(ByVal column As String) As String

        ' By default, set the sort direction to ascending.
        Dim sortDirection = "DESC"

        ' Retrieve the last column that was sorted.
        Dim sortExpression = TryCast(ViewState("SortExpression"), String)

        If sortExpression IsNot Nothing Then
            ' Check if the same column is being sorted.
            ' Otherwise, the default value can be returned.
            If sortExpression = column Then
                Dim lastDirection = TryCast(ViewState("SortDirection"), String)
                If lastDirection IsNot Nothing _
                  AndAlso lastDirection = "DESC" Then

                    sortDirection = "ASC"

                End If
            End If
        End If

        ' Save new values in ViewState.
        ViewState("SortDirection") = sortDirection
        ViewState("SortExpression") = column
        Return sortDirection
    End Function

#End Region

    Private Sub SaveLogSearch()
        Try
            clsSessionMenuRight = Session("ClsMenuRight")
            Dim desc As String = ""

            If Not (ddlBranch.SelectedValue = "0") Then
                desc += " ส่วนงาน/สาขา - " + ddlBranch.SelectedItem.Text
            End If

            If Not (txtDateStart.Text = "") And Not (txtDateEnd.Text = "") Then
                desc += " วันที่ทำธุรกรรม - " + txtDateStart.Text + " ถึง " + txtDateEnd.Text
            End If


            If Not desc = "" Then
                desc = "ค้นหาข้อมูลผู้ใช้งานระบบ จาก " + desc
            Else
                desc = "ค้นหาทั้งหมด "
            End If

            Dim clsLogTransaction As New clsLogTransaction
            clsLogTransaction.saveLogTransaction("", clsSessionMenuRight.getUserName, 1, desc, strMenuCode)
            clsLogTransaction = Nothing
        Catch ex As Exception
            Throw ex
        End Try

    End Sub

End Class
